package com.chouxiaozi.mvc;

import com.chouxiaozi.mvc.controller.MemberController;
import com.chouxiaozi.mvc.controller.OrderController;
import com.chouxiaozi.mvc.controller.SystemController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class DispatcherServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doDispatch(req, resp);
    }

    private void doDispatch(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String uri = req.getRequestURI();
        String mid = req.getParameter("mid");

        String result = "404 NOT FOUND";
        if(uri.contains("getMemberById")){
            result = new MemberController().getMemberById(mid);
        }else if(uri.contains("getOrderById")){
            result = new OrderController().getOrderById(mid);
        }else if(uri.contains("logout")){
            result = new SystemController().logout(mid);
        }
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(result);
    }
}
